package com.hex.liyue.nearmodule.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.fragment.BaseFragment;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.lookingdatemodule.activity.InSomeDetailsActivity;
import com.hex.liyue.nearmodule.adapter.NearListAdapter;
import com.hex.liyue.nearmodule.bean.NearListBean;
import com.hex.liyue.nearmodule.presenter.INearVIew;
import com.hex.liyue.nearmodule.presenter.NearPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;


/**
 * Created by WJ on 2016/6/12.
 * <p/>
 * 附近约会的界面
 */
public class FragmentNear extends BaseFragment implements YRecycleview.RefreshAndLoadMoreListener, AMapLocationListener, INearVIew {

    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.rv_near)
    YRecycleview rvNear;
    private AMapLocationClient mlocationClient;
    private AMapLocationClientOption mLocationOption;
    private int page = 1;
    private int pagernum = 15;
    private String gpsx;
    private String gpsy;
    private NearPresenter nearPresenter;
    private List<NearListBean.DataBean> nearlist = new ArrayList();
    private NearListAdapter nearListAdapter;

    @Override
    public void initdata(Bundle arguments) {
        showLod("加载中");
        //取消处理自己的逻辑
        mlocationClient = new AMapLocationClient(getActivity());
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);

        //设置定位监听
        mlocationClient.setLocationListener(this);
        //设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        // 启动定位
        mlocationClient.startLocation();
        rvNear.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvNear.setRefreshAndLoadMoreListener(this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_near;
    }

    @Override
    protected void bindPresenter() {
        nearPresenter = new NearPresenter(this);
    }

    @Override
    protected void initToolbar() {
        tvToobarTitle.setText("附近的约会");
        tvToobarTitle.setVisibility(View.VISIBLE);
    }

    @Override
    public void onRefresh() {
        if (TextUtils.isEmpty(gpsx) || TextUtils.isEmpty(gpsy)) {
            showT("定位中请稍后再试");
            rvNear.setReFreshComplete();
            return;
        }
        page = 1;
        //nearPresenter.getList(page, pagernum, gpsx, gpsy);
        mlocationClient.startLocation();
    }

    @Override
    public void onLoadMore() {
        page++;
        nearPresenter.getList(page, pagernum, gpsx, gpsy);
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation.getErrorCode() == 0) {
            //定位成功回调信息，设置相关消息
            gpsy = String.valueOf(aMapLocation.getLatitude());
            gpsx = String.valueOf(aMapLocation.getLongitude());
            nearPresenter.getList(page, pagernum, gpsx, gpsy);
        } else {
            //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
            L.e("AmapError", "location Error, ErrCode:"
                    + aMapLocation.getErrorCode() + ", errInfo:"
                    + aMapLocation.getErrorInfo());
        }
        mlocationClient.stopLocation();//结束定位
    }

    @Override
    public void setAdApter(NearListBean nearListBean) {
        if (nearListBean != null && nearListBean.data.size() > 0) {
            if (page == 1) {
                nearlist.clear();
            }
            for (NearListBean.DataBean dataBean : nearListBean.data) {
                nearlist.add(dataBean);
            }
        }
        if (nearListAdapter == null) {
            nearListAdapter = new NearListAdapter(nearlist);
            nearListAdapter.setLoaction(gpsx,gpsy);
            rvNear.setAdapter(nearListAdapter);
            nearListAdapter.setOnItemClickListener(new NearListAdapter.OnItemClickListener() {
                @Override
                public void onItemClickListener(String appointment_id) {
                    Intent intent = new Intent(getActivity(), InSomeDetailsActivity.class);
                    intent.putExtra("appointment_id", appointment_id);
                    getActivity().startActivity(intent);
                }
            });
        }
        nearListAdapter.notifyDataSetChanged();
    }

    @Override
    public void setComplete() {
        rvNear.setReFreshComplete();
        rvNear.loadMoreComplete();
        closeLod();
    }

    @Override
    public void showLod(String msg) {
        layoutLoading();
    }

    @Override
    public void closeLod() {
        closeLoad();
        layoutContent();
    }

    @Override
    public void showT(String msg) {
        showToast(getActivity(), msg);
    }
}

